package com.factory.iamp.dao;

import com.factory.iamp.pojo.Sap;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SapMapper {
    @Select("select count(order_id) from sap")
    int Count(@Param("lid") int lid);

    @Select("select * from sap limit #{offset}, #{limit}")
    List<Sap> ListPaged(@Param("offset") int offset, @Param("limit") int limit, @Param("lid") int lid);

    @Select("select * from sap where order_id = #{order_id}")
    Sap getSapByOrderId(@Param("order_id") String order_id, @Param("lid") int lid);

    @Select("select count(order_id) from sap where WERKS like '%${findstr}%'")
    int findCount(@Param("findstr") String findstr, @Param("lid") int lid);

    @Select("select * from sap where WERKS like '%${findstr}%' limit #{offset}, #{limit}")
    List<Sap> findSap(
            @Param("findstr") String findstr, @Param("lid") int lid,
            @Param("offset") int offset, @Param("limit") int limit
    );

    @Insert({
            "insert into sap(order_id, lid, AUART, WERKS, MATNR, GAMNG, GWEMG, GSTRP, GLTRP, KDAUF, KDPOS, KUNNR) ",
            "values(#{order_id}, #{lid}, #{data.AUART}, #{data.WERKS}, #{data.MATNR}, #{data.GAMNG}, #{data.GWEMG}, ",
            "#{data.GSTRP}, #{data.GLTRP}, #{data.KDAUF}, #{data.KDPOS}, #{data.KUNNR})"})

    void addSap(@Param("order_id") String order_id, @Param("lid") int lid, @Param("data") Sap data);

    @Delete("delete from sap where order_id = #{order_id}")
    void deleteSap(@Param("order_id") String order_id, @Param("lid") int lid);
}
